package com.wc.算法提高课.A第一章_动态规划.状态机模型.股票买卖IV;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/10 16:15
 * @description https://www.acwing.com/problem/content/1059/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 110, INF = 0x3f3f3f3f;
    // f[i][j][k] 表示第i天交易了j次股票状态为k的最大值, k = 0, 表示手中无货, k = 1表示手中有货
    static int[][] f = new int[M][2];
    static int[] w = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();

        for(int i = 1; i <= n; i++) w[i] = sc.nextInt();
        for (int j = 0; j <= m; j++) {
            Arrays.fill(f[j], -INF);
        }

        // 初始化每天都不买无交易肯定是0
        f[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                // 手中无货就是前面也没货或者是前面有货把卖了
                f[j][0] = Math.max(f[j][0], f[j][1] + w[i]);
                // 手中有货就是前面也有货或者是前面没货把货买上
                f[j][1] = Math.max(f[j][1], f[j - 1][0] - w[i]);
            }
        }
        int res = 0;
        for (int i = 0; i <= m; i++) res = Math.max(res, f[i][0]);
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
